//
//  YZMThreeQutoesLandScapeCell.m
//  YZMicroStock
//
//  Created by apple on 2017/11/29.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMThreeQutoesLandScapeCell.h"
#import "YZMTrendView.h"
#import "YZMStockRealModel.h"
#import "YZMTrendPacket.h"
#import "YZMAppDelegate.h"
#import "NSNumber+Utils.h"
#import <NSDate+DateTools.h>

@interface YZMThreeQutoesLandScapeCell ();

@property (weak, nonatomic) IBOutlet UILabel *labelName;

@property (weak, nonatomic) IBOutlet UILabel *labelCode;

@property (weak, nonatomic) IBOutlet UILabel *labelQutoes;

@property (weak, nonatomic) IBOutlet UILabel *labelProfitl;

@property (weak, nonatomic) IBOutlet UILabel *labelRange;

@property (weak, nonatomic) IBOutlet UIView *viewLeft;

@property (weak, nonatomic) IBOutlet UIView *viewRight;

@property (weak, nonatomic) IBOutlet YZMTrendView *trendView;

@property (weak, nonatomic) IBOutlet UILabel *labelDate;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation YZMThreeQutoesLandScapeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.backgroundColor = [YZMAppStyle colorForMain];
    self.contentView.backgroundColor = [YZMAppStyle colorForMain];
    self.labelName.textColor = [YZMAppStyle colorForWhite];
    self.labelCode.textColor = [YZMAppStyle colorForLightGrayText];
    self.viewLeft.backgroundColor = [YZMAppStyle colorForMain];
    self.viewRight.backgroundColor = [YZMAppStyle colorForMain];
    self.labelDate.textColor = [YZMAppStyle colorForWhite];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(refreshDate) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    [self.timer fire];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setTrendRotation:)];
//    tap.numberOfTapsRequired = 2;
//    [self.trendView addGestureRecognizer:tap];
}

- (void)refreshDate{
    self.labelDate.text = [NSString stringWithFormat:@"时间：%@",[[NSDate date] formattedDateWithFormat:@"HH:mm"]];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)clickedBtnClose:(id)sender {
    [self setTrendRotation:nil];
}

- (void)setRealInfo:(YZMStockRealModel *)realInfo{
    //    self.infoView.realStock = realInfo;
    self.labelName.text = realInfo.name;
    self.labelCode.text = realInfo.code;
    if (realInfo.name.length == 0) {
        self.labelName.text = @"--";
    }
    if (realInfo.code.length == 0) {
        self.labelCode.text = @"--";
    }
    self.labelQutoes.text = @(realInfo.price).p02fString;
    double distance = realInfo.price - realInfo.lastClosePrice;
    if ([@(distance) isEqualWithDouble:0.0]) {
        self.labelQutoes.textColor = [YZMAppStyle colorForGrayText];
        self.labelProfitl.textColor = [YZMAppStyle colorForGrayText];
        self.labelRange.textColor = [YZMAppStyle colorForGrayText];
        self.labelProfitl.text = @"+0.00";
        self.labelRange.text = @"+0.00%";
        
    }else if([@(distance) isGreaterThanDouble:0.0]){
        
        self.labelQutoes.textColor = [YZMAppStyle colorForRiseText];
        self.labelProfitl.textColor = [YZMAppStyle colorForRiseText];
        self.labelRange.textColor = [YZMAppStyle colorForRiseText];
        self.labelProfitl.text = [NSString stringWithFormat:@"+%@",@(distance).p02fString];
        self.labelRange.text = [NSString stringWithFormat:@"+%@%%",@(distance*100/realInfo.lastClosePrice).p02fString];
        
    }else{
        self.labelQutoes.textColor = [YZMAppStyle colorForDropText];
        self.labelProfitl.textColor = [YZMAppStyle colorForDropText];
        self.labelRange.textColor = [YZMAppStyle colorForDropText];
        self.labelProfitl.text = [NSString stringWithFormat:@"%@",@(distance).p02fString];
        self.labelRange.text = [NSString stringWithFormat:@"%@%%",@(distance*100/realInfo.lastClosePrice).p02fString];
    }
}


- (void)setTrendPacket:(YZMTrendPacket *)trendPacket{
    [self.trendView updateFromDataPacket:trendPacket];
}

- (void)setTrendRotation:(UITapGestureRecognizer *)tap{
    [[UINavigationBar appearance] setHidden:NO];
    YZMAppDelegate * appDelegate = (YZMAppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = NO;
    NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
    //强制竖屏
    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}

- (void)stopTimer{
    [self.timer invalidate];
    self.timer = nil;
}


@end
